<?php

class Booking extends CI_Model {

    function index() {
        $q = $this->db->get('booking');
        return $q->result();
    }

    function show($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('booking');
        return $q->result();
    }

    function edit($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('booking');
        return $q->result();
    }

    function create($bank_name, $an, $no_rek) {
        $data = array(
            'bank_name' => $bank_name,
            'an' => $an,
            'no_rekening' => $no_rek,
        );
        $this->db->insert('booking', $data);
        redirect('bookings/index');
    }

    function update($id, $bank_name, $an, $no_rekening) {
        $data = array(
            'bank_name' => $bank_name,
            'an' => $an,
            'no_rekening' => $no_rekening,
        );
        $this->db->where('id', $id);
        $this->db->update('booking', $data);
        redirect('bookings/index');
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('booking');
        redirect('bookings/index');
    }

    function code_booking($id_booking, $id_user, $fee, $total, $code_booking, $file) {
        $this->db->where('id', $id_booking);
        $data = array(
            'fee' => $fee,
            'total' => $total,
            'code_booking' => $code_booking,
            'status' => 3,
            'file' => $file,
        );
        $this->db->update('booking', $data);
//        redirect('bookings/show/' . $id_booking);
        return true;
    }
    
    function edit_code_booking($id_booking, $id_user, $flight_number, $price_adult, $price_child, 
                        $price_infant, $simple_departure_time, $simple_arrival_time, $class, $fee, $total, $total_price, $code_booking, $keterangan, $status, $file) {
        $this->db->where('id', $id_booking);
        
        $data = array(
            'flight_number' => $flight_number,
            'price_value' => $total_price,
            'price_adult' => $price_adult,
            'price_child' => $price_child,
            'price_infant' => $price_infant,
            'simple_departure_time' => $simple_departure_time,
            'simple_arrival_time' => $simple_arrival_time,
            'class' => $class,
            'fee' => $fee,
            'total' => $total,
            'code_booking' => $code_booking,
            'keterangan' => $keterangan,
            'status' => $status,
            'file' => $file,
            
        );
        $this->db->update('booking', $data);
        return true;
    }

    function get_user($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('costumer');
        foreach ($q->result() as $row) {
            $user = $row->username;
        }
        return $user;
    }

    function get_adult($code) {
        $this->db->where('code', $code);
        $q = $this->db->get('booking_adult');
        return $q->result();
    }

    function get_child($code) {
        $this->db->where('code', $code);
        $q = $this->db->get('booking_child');
        return $q->result();
    }

    function get_infant($code) {
        $this->db->where('code', $code);
        $q = $this->db->get('booking_infant');
        return $q->result();
    }

    public function record_count() {
        return $this->db->count_all("booking");
    }

    function fetch_booking($limit, $start) {
        $this->db->order_by('id', 'DESC');
        $this->db->limit($limit, $start);
        $query = $this->db->get("booking");
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    function confirm_booking($id) {
        $this->db->where('id', $id);
        $data = array(
            'status' => '1'
        );
        $this->db->update('booking', $data);
        redirect("costumers/show_book/" . $id . '.confirm');
    }

    function cancel_booking($id, $code) {
        $this->cancel_adult($code);
        $this->cancel_child($code);
        $this->cancel_infant($code);

        $this->db->where('id', $id);
        $this->db->delete('booking');
        redirect("costumers/status_book");
    }

    function cancel_adult($code) {
        $this->db->where('code', $code);
        $this->db->delete('booking_adult');
        return true;
    }

    function cancel_child($code) {
        $this->db->where('code', $code);
        $this->db->delete('booking_child');
        return true;
    }

    function cancel_infant($code) {
        $this->db->where('code', $code);
        $this->db->delete('booking_infant');
        return true;
    }

    function data_benar($id) {
        $this->db->where('id', $id);
        $data = array(
            'status' => 2,
        );
        $this->db->update('booking', $data);
        return true;
    }
    
    function get_booking($date){
        $this->db->like('date_create', $date);
        $q = $this->db->get('booking');
        return $q->result();
    }
    
//    function crone_delete($code) {
//        $this->cancel_adult($code);
//        $this->cancel_child($code);
//        $this->cancel_infant($code);
//
//        $this->db->where('code', $code);
//        $this->db->delete('booking');
//    }
    
    function crone_delete($code) {
        $this->db->where('code', $code);
        $data = array('status' => '4');
        $this->db->update('booking', $data);
    }

}

?>
